package com.ruoyi.service.impl;

import java.util.List;

import com.ruoyi.domain.Book;
import com.ruoyi.domain.dto.ShelvedBookDto;
import com.ruoyi.mapper.BookMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.ShelvedBookMapper;
import com.ruoyi.domain.ShelvedBook;
import com.ruoyi.service.IShelvedBookService;
import org.springframework.validation.annotation.Validated;

/**
 * 上架Service业务层处理
 *
 * @author mjx
 * @date 2024-03-12
 */
@Service
public class ShelvedBookServiceImpl implements IShelvedBookService
{
    @Autowired
    private ShelvedBookMapper shelvedBookMapper;
    @Autowired
    private BookMapper bookMapper;

    /**
     * 查询上架
     *
     * @param shelvedId 上架主键
     * @return 上架
     */
    @Override
    public ShelvedBook selectShelvedBookByShelvedId(Long shelvedId)
    {
        return shelvedBookMapper.selectShelvedBookByShelvedId(shelvedId);
    }

    /**
     * 查询上架列表
     *
     * @param shelvedBook 上架信息
     * @return 上架
     */
    @Override
    public List<ShelvedBook> selectShelvedBookList(ShelvedBook shelvedBook)
    {
        return shelvedBookMapper.selectShelvedBookList(shelvedBook);
    }

    /**
     * 查询上架信息
     *
     * @param shelvedBookDto 上架
     * @return 上架
     */
    @Override
    public List<ShelvedBookDto> selectList(ShelvedBookDto shelvedBookDto)
    {
        return shelvedBookMapper.selectList(shelvedBookDto);
    }

//    /**
//     * 查询图书信息
//     *
//     * @param shelvedBook 上架
//     * @return 上架
//     */
//    @Override
//    public List<ShelvedBook> selectBookList(ShelvedBook shelvedBook)
//    {
//        return shelvedBookMapper.selectBookList(shelvedBook);
//    }

    /**
     * 新增上架
     *
     * @param shelvedBook 上架
     * @return 结果
     */
    @Override
    public int insertShelvedBook(ShelvedBook shelvedBook)
    {
        shelvedBook.setCreateTime(DateUtils.getNowDate());
        return shelvedBookMapper.insertShelvedBook(shelvedBook);
    }

    /**
     * 修改上架
     *
     * @param shelvedBook 上架
     * @return 结果
     */
    @Override
    public int updateShelvedBook(ShelvedBook shelvedBook)
    {
        shelvedBook.setUpdateTime(DateUtils.getNowDate());
        return shelvedBookMapper.updateShelvedBook(shelvedBook);
    }

    /**
     * 批量删除上架
     *
     * @param shelvedIds 需要删除的上架主键
     * @return 结果
     */
    @Override
    public int deleteShelvedBookByShelvedIds(Long[] shelvedIds)
    {
        return shelvedBookMapper.deleteShelvedBookByShelvedIds(shelvedIds);
    }

    /**
     * 删除上架信息
     *
     * @param shelvedId 上架主键
     * @return 结果
     */
    @Override
    public int deleteShelvedBookByShelvedId(Long shelvedId)
    {
        return shelvedBookMapper.deleteShelvedBookByShelvedId(shelvedId);
    }


    /**
     *
     * @param shelvedBook 上架书本
     * @return
     */
    @Override
    public String upShelve(@Validated ShelvedBook shelvedBook) {
        Book book = bookMapper.selectBookByBookId(shelvedBook.getBookId());
        if (book == null) return "请输入正确的图书ID";
        //查询库存数量
        //库存不足
        if (shelvedBook.getShelvedNumber() > book.getNumber()) {
            return "库存不足";
        }else {
            //判断该书是否已经在书架
            Book book1 = new Book();
            book1.setBookId(shelvedBook.getBookId());
            book1.setNumber((long) shelvedBook.getShelvedNumber()*-1);
            ShelvedBook shelvedBookResult = shelvedBookMapper.isBookExist(shelvedBook.getBookId());
            if (shelvedBookResult == null){
                //未在书架
                shelvedBookMapper.insertShelvedBook(shelvedBook);
                bookMapper.changeBookNum(book1);
            }else {
                //已在书架
                //更改书架数量和仓库数量
                shelvedBookMapper.changeShelveNum(shelvedBook);
                bookMapper.changeBookNum(book1);
            }
            return "上架完成";
        }
    }


    /**
     *
     * @param shelvedBook 下架书本
     * @return
     */

    @Override
    public String offShelve(@Validated ShelvedBook shelvedBook) {
        Book book = bookMapper.selectBookByBookId(shelvedBook.getBookId());
        ShelvedBook shelvedBook1 = shelvedBookMapper.isBookExist(shelvedBook.getBookId());
        //查询库存是否含有此书
        if (book == null) return "请输入正确的图书ID";
        if (shelvedBook1 == null) return "书架上没有此书";

        //查询书架数量
        if (shelvedBook.getShelvedNumber() > shelvedBook1.getShelvedNumber()) {
            //书架书本不足
            return "书架上书本数量不足";
        }else {
            //书架书本数量足够
            //更改书架数量和仓库数量
            Book book1 = new Book();
            book1.setBookId(shelvedBook.getBookId());
            book1.setNumber((long) shelvedBook.getShelvedNumber());
            bookMapper.changeBookNum(book1);

            shelvedBook.setShelvedNumber(shelvedBook.getShelvedNumber()*-1);
            shelvedBookMapper.changeShelveNum(shelvedBook);

            return "下架完成";
        }
    }

    @Override
    public Integer selectBookNum() {
        Integer bookNum = shelvedBookMapper.getBooKNum();
        return bookNum;
    }

    /**
     * 保存当天库存数量到表中去
     * @param bookNum
     */
    @Override
    public void setBookNum(Integer bookNum) {
        shelvedBookMapper.setBookNum(bookNum);
    }
}

